﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using SVC.client;
using System.Linq.Expressions;
namespace UI.Forms
{
    public partial class ProductSale_M_List : FormBase1
    {

        BindingList<View_ProductSale_M> mlist;//销售单据
        public delegate void DoAction(ProductSale_M cs);//如果有参数的话，定义参数 
        public DoAction FatherRefresh;
        private string comefrom = ""; //来源,为空就是直接打开销售单据列表

        public ProductSale_M_List()
        {
            InitializeComponent();
        }

        public ProductSale_M_List(string comefrom)
        {
            InitializeComponent();
            this.comefrom = comefrom;
        }

        private void Select_BC_Load(object sender, EventArgs e)
        {
            if (comefrom == "back") //从退货单中打开
            {
                layoutControlItem1.Text = "双击选择销售出库单";
                bar1.Visible = false; //隐藏菜单栏
            }
            bind();
        }

        private void bind()
        {
            string field = cmbField.Text;
            string logic = cmbLogic.Text ;
            string value = "";
            if (this.cmbField.Text == "单据日期")
            {
                value = DateTime.Parse(dateEdit1.Text).ToString("yyyy-MM-dd");
            }
            else
            {
                value = textEdit1.Text.Trim();
            }
            mlist = my.bw.GetProductSale_M_ByCondition(field, logic, value,my.ACID);
            gridControl1.DataSource = mlist;
        }

        private void btn_sx_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }

        private void btn_OK_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ProductSale frm = new ProductSale("");
            frm.ShowDialog();
            bind();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            bind();
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            cmbField.EditValue = null;
            cmbLogic.EditValue = null;
            textEdit1.EditValue = null;
            dateEdit1.EditValue = null;
            dateEdit1.Visible = false;
            textEdit1.Visible = true;
            bind();
        }

        private void cmbField_SelectedValueChanged(object sender, EventArgs e)
        {
            if (this.cmbField.Text=="单据日期")
            {
                dateEdit1.Visible = true;
                textEdit1.Visible = false;
            }
            else
            {
                dateEdit1.Visible = false;
                textEdit1.Visible = true;
            }

        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            if (comefrom == "")
            {
                GotoUpdate();
            }
            if (comefrom == "back")//退货中打开，双击就是选择
            {
                string djn=gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, gridColumn1).ToString();
                ProductSale_M psmitem = my.bw.GetPrductSale_M_ByID(djn, my.ACID);
                psmitem.ProductSale_D = my.bw.Get_ProductSale_DList_By_M(djn, my.ACID);
                FatherRefresh(psmitem);
                this.Close();
            }
        }

        //修改
        private void GotoUpdate()
        {
            string djn = gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, gridColumn1).ToString();
            ProductSale frm = new ProductSale(djn);
            frm.ShowDialog();
            bind();
        }

        private void btn_update_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            GotoUpdate();
        }

        private void btn_Delete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                string djn = gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, gridColumn1).ToString();
                decimal ysmoney = decimal.Parse(gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, gridColumn11).ToString());
                if (ysmoney > 0)
                {
                    Comm.ShowAlert("销售出库单【" + djn + "】已收款，不能删除！");
                    return;
                }
                if (XtraMessageBox.Show("您确定要删除销售出库单【" + djn + "】？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
                {
                    string r = my.bw.ProductSale_Delete(djn, my.ACID);
                    if (string.IsNullOrEmpty(r))
                    {
                        Comm.ShowInfo("销售出库单删除成功！");
                        bind();
                    }
                }
            }
            catch (Exception ex)
            {
                Comm.ShowErorr("错误:" + ex.Message);
            }

        }

    }
}